<template>
  <div>
    <!-- -------------------修改form表单-------------------- -->
    <el-dialog title="修改用户" :visible.sync="editUserFormVisible" width="50%" @close="editFormClose">
      <!-- form主体内容 -->
      <el-form
        :model="editUserForm"
        :rules="editUserFormRules"
        ref="editUserFormRef"
        label-width="80px"
      >
        <el-form-item label="用户名" size="small">
          <el-input v-model="editUserForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱号" prop="email" size="small">
          <el-input v-model="editUserForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile" size="small">
          <el-input v-model="editUserForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <!-- form底部按钮 -->
      <span slot="footer">
        <el-button @click="editUserFormVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="submitEditForm" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["editUserForm"],
  data() {
     //****************************************************************************验证规则通用 */
    // 验证邮箱的校验规则
    let checkEmail = (rule, value, callback) => {
      // 验证邮箱的正则表达式
      const regEmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

      if (regEmail.test(value)) {
        // 合法的邮箱
        return callback();
      }
      // 不合法的邮箱
      callback(new Error("请输入合法的邮箱"));
    };

    // 验证手机号的校验规则
    let checkMobile = (rule, value, callback) => {
      // 验证手机号的正则表达式
      const regMobile = /^1(3|4|5|6|7|8|9)\d{9}$/;

      if (regMobile.test(value)) {
        // 合法的手机号
        return callback();
      }
      // 不合法
      callback(new Error("请输入合法的手机号"));
    };
    return {
      editUserFormRules: {
        email: [
          { required: true, message: "请输入电子邮箱", trigger: "blur" },
          { validator: checkEmail, trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          { validator: checkMobile, trigger: "blur" },
        ],
      },
      editUserFormVisible: false,
    };
  },
  created() {},
  methods: {
    // 显示弹出框
    showDialog() {
      this.editUserFormVisible = true;
    },
    //监听修改用户关闭事件，重置表单
    editFormClose() {
      this.$refs.editUserFormRef.resetFields();
      this.editUserFormVisible = false;
    },
    //提交修改用户表单加预验证功能
    submitEditForm() {
      this.$refs.editUserFormRef.validate((valid) => {
        if (valid) {
          this.$axios
            .put("users/" + this.editUserForm.id, {
              email: this.editUserForm.email,
              mobile: this.editUserForm.mobile,
            })
            .then((res) => {
              // 提示修改用户成功
              this.$message.success("修改用户成功");
              this.$emit("refreshMethod",true)
              //关闭对话窗口
              this.editUserFormVisible = false;
            })
            .catch((error) => {});
        } else {
          this.$message.error("预校验失败");
          return;
        }
      });
    },
  },
};
</script>
 
<style scoped>
</style>